package com.dabai.uitools.utils;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dabai.uitools.impl.OnDragDropToDesignatedAreaListener;
import com.google.android.material.textfield.TextInputLayout;

/**
 * 视图 辅助工具类
 */
public class ViewUtils {

    /**
     * 设置 长按拖动视图到指定区域
     *
     * @param vibration                          振动
     * @param dragView                           拖动视图
     * @param specifyView                        指定视图
     * @param onDragDropToDesignatedAreaListener 在阻力降至指定区域听众
     */
    @SuppressLint("ClickableViewAccessibility")
    public static void setLongClickToDragViewToDesignatedArea(View dragView, View specifyView, boolean vibration, OnDragDropToDesignatedAreaListener onDragDropToDesignatedAreaListener) {


        dragView.setOnLongClickListener(v -> {

            onDragDropToDesignatedAreaListener.press(dragView,specifyView);

            final float rawX = dragView.getX();
            final float rawY = dragView.getY();

            if (vibration) {
                dragView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
            }

            dragView.setOnTouchListener((v1, event) -> {


                switch (event.getAction()) {

                    case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动对应的事件

                        v1.setX((v1.getX() - ((float) v1.getWidth() / 2)) + event.getX());
                        v1.setY((v1.getY() - ((float) v1.getHeight() / 2)) + event.getY());

                        onDragDropToDesignatedAreaListener.onMove(
                                whetherToOverlapArea(dragView,specifyView),dragView,specifyView);

                        break;
                    case MotionEvent.ACTION_UP:// 手指在屏幕上移动对应的事件

                        onDragDropToDesignatedAreaListener.release(
                                whetherToOverlapArea(dragView,specifyView),dragView,specifyView);

                        dragView.setX(rawX);
                        dragView.setY(rawY);
                        dragView.setOnTouchListener(null);
                        break;
                }
                return true;
            });

            return true;
        });

    }


    /**
     * 是否重叠区域(不准确重叠 检测)
     *
     * @param view1 view1
     * @param view2 view2
     * @return boolean
     */
    public static boolean whetherToOverlapArea(View view1, View view2){

        if (view1==null || view2 == null){
            return false;
        }

       //左上角 x
        float corner1x = view2.getX();
        //左上角 y
        float corner1y = view2.getY();

        //右下角 x
        float corner2x = view2.getX()+view2.getWidth();
        //右下角 y
        float corner2y = view2.getY()+view2.getHeight();

        //view1 的坐标
        float v1x = view1.getX();
        float v1y = view1.getY();

        int halfWidth = view1.getWidth()/2;
        int halfHeight = view1.getHeight()/2;

        //判断在不在 4个角以内
        return (v1x+halfWidth > corner1x && v1y+halfHeight > corner1y) && (v1x+halfWidth < corner2x && v1y+halfHeight < corner2y);
    }


    /**
     * 是否重叠区域(准确重叠 检测)
     *
     * @param view1 view1
     * @param view2 view2
     * @return boolean
     */
    public static boolean realWhetherToOverlapArea(View view1, View view2){

        if (view1==null || view2 == null){
            return false;
        }

       //左上角 x
        float corner1x = view2.getX();
        //左上角 y
        float corner1y = view2.getY();

        //右下角 x
        float corner2x = view2.getX()+view2.getWidth();
        //右下角 y
        float corner2y = view2.getY()+view2.getHeight();

        //view1 的坐标
        float v1x = view1.getX();
        float v1y = view1.getY();

        int vw = view1.getWidth();
        int vh = view1.getHeight();

        //判断在不在 4个角以内
        return (v1x+vw > corner1x && v1y+vh > corner1y) && (v1x < corner2x && v1y < corner2y);
    }


    /**
     * 设置 长按拖动视图
     *
     * @param view      视图
     * @param vibration 振动
     */
    @SuppressLint("ClickableViewAccessibility")
    public static void setLongClickToDragView(View view, boolean vibration) {

        view.setOnLongClickListener(v -> {

            if (vibration) {
                view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
            }

            view.setOnTouchListener((v1, event) -> {
                switch (event.getAction()) {

                    case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动对应的事件

                        v1.setX((v1.getX() - ((float) v1.getWidth() / 2)) + event.getX());
                        v1.setY((v1.getY() - ((float) v1.getHeight() / 2)) + event.getY());

                        break;
                    case MotionEvent.ACTION_UP:// 手指在屏幕上移动对应的事件
                        view.setOnTouchListener(null);
                        break;
                }
                return true;
            });

            return true;
        });

    }


    /**
     * 智能设置弹窗窗口的位置
     *
     * @param context     上下文
     * @param view        视图
     * @param alertDialog 警告对话框
     */
    public static void smartSettingsWindowPosition(Context context, View view, AlertDialog alertDialog) {
        int screenThree = DisplayUtils.getScreenHeight(context) / 3 - 150;
        int positionY = (int) view.getY() + view.getHeight() / 2;

        if (positionY < screenThree) {
            alertDialog.getWindow().setGravity(Gravity.TOP);
        } else if (positionY > screenThree * 2) {
            alertDialog.getWindow().setGravity(Gravity.BOTTOM);
        }

    }


    /**
     * 设置视图的高度
     *
     * @param view 视图
     * @param px   px
     */
    public static void setViewHeight(View view, int px) {
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        linearParams.height = px;
        view.setLayoutParams(linearParams);
    }

    /**
     * 设置视图的宽度
     *
     * @param view 视图
     * @param px   px
     */
    public static void setViewWidth(View view, int px) {
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        linearParams.width = px;
        view.setLayoutParams(linearParams);
    }


    /**
     * 解析布局
     *
     * @param context 上下文
     * @param layout  布局
     * @return {@link View}
     */
    public static View inflateLayout(Context context, int layout) {
        return LayoutInflater.from(context).inflate(layout, null);
    }

    public static String getTextInputLayoutText(@NonNull TextInputLayout textInputLayout) {
        return textInputLayout.getEditText().getText().toString();
    }

    public static void setTextInputLayoutText(@NonNull TextInputLayout textInputLayout,String text) {
        textInputLayout.getEditText().setText(text);
    }

    /**
     * 文本视图println
     *
     * @param textView 文本视图
     * @param text     文本
     */
    @SuppressLint("SetTextI18n")
    public static void textViewPrintln(TextView textView, String text){
        String str1 = textView.getText().toString();

        if (str1.isEmpty()){
            textView.setText(str1);
        }else {
            textView.setText(str1+"\n"+text);
        }
    }

    /**
     * 获取View在当前窗口内的绝对坐标
     * obtain the absolute coordinates to obtain the View within the current window
     *
     * @param view view
     * @return int[] x y
     */
    public static int[] getLocationInWindow(View view) {
        int[] location = new int[2];
        view.getLocationInWindow(location);
        return location;
    }

    /**
     * 获取View在整个屏幕内的绝对坐标
     * obtain access to View the absolute coordinates of within the entire screen
     *
     * @param view view
     * @return int[] x y
     */
    public static int[] getLocationOnScreen(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        return location;
    }


    /**
     * 根据view获取bitmap
     *
     * @param view 视图
     * @return {@link Bitmap}
     */
    public static Bitmap getBitmapByView(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
                Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }


    /**
     * Drawable 转换为 Bitmap
     *
     * @param drawable 可拉的
     * @return {@link Bitmap}
     */
    public static Bitmap drawableToBitmap(Drawable drawable) {

        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }


}
